<script>
import jwtDecode from 'jwt-decode'
import { getWechatCode, autoLogin } from '@/api/login/wechat.js'
import { getSignIn, getSignIn2 } from '@/api/personalCenter/index.js'
import { salesmanGetSignIn2 } from '@/api/invoicer/index.js'
import authority from '@/components/permission/authority.js'
import { bindAliasAndTag } from '@/utils/push.js'
import { encrypt_oaid, decrypt_oaid } from '@/utils/cryptography.js'

export default {
  onLaunch () {
    let m = uni.getStorageSync('machine')
    if (!m) {
      let machine = uni.getSystemInfoSync()
      machine.deviceId = encrypt_oaid(machine.deviceId)
      uni.setStorageSync('machine', machine)
    }
    // #ifdef MP
    //小程序版本号
    uni.setStorageSync('version', '8.4.2')
    // #endif
    // #ifndef MP
    if (uni.getSystemInfoSync().osName === 'ios') {
      authority.judgeIosPermission('push')
    } else {
      authority.requestAndroidPermission('android.permission.POST_NOTIFICATIONS')
    }
    // 推送 设备id
    console.log(plus.push.getClientInfo())
    console.log('cid')
    bindAliasAndTag('', ['恶霸'])
    // 推送 接受消息
    uni.onPushMessage(res => {
      console.log('收到推送消息：', res)
    })
    plus.runtime.getProperty(plus.runtime.appid, info => {
      uni.setStorageSync('version', info.version)
    })
    let agree = uni.getStorageSync('agree')
    const platform = uni.getSystemInfoSync().osName
    console.log(platform)
    if (!agree && platform !== 'android') {
      uni.navigateTo({
        url: '/pages/iosPrivacy/index',
        success () {
          plus.navigator.closeSplashscreen()
        }
      })
    }
    uni.setStorageSync('advertisementIndex', 0)
    // #endif
    if (uni.getStorageSync('token')) {
      let a = jwtDecode(uni.getStorageSync('token'))
      let b = JSON.parse(a.userContext).role === 'STAFF' ? true : false

      if (parseInt(new Date().getTime() / 1000) > parseInt(a.exp)) {
        if (b) {
          this.salespersonLogin()
        } else {
          this.logInAgain()
        }
      } else {
        if (b) {
          setTimeout(() => {
            uni.reLaunch({ url: '/invoicer/index' })
          }, 1000)
        } else {
          this.$isResolve()
        }
      }
    } else {
      this.logInAgain()
    }
  },
  onShow: function () { },
  onHide: function () { },
  methods: {
    async logInAgain () {
      uni.removeStorageSync('token')
      // #ifdef MP-WEIXIN
      const code = await getWechatCode()
      if (code) {
        const res = await autoLogin({
          code: code
        })
        if (res.code === 200) {
          uni.showToast({
            icon: 'none',
            title: '登录成功',
            mask: true
          })
          uni.setStorageSync('token', res.data ? res.data.accessToken : '')
          this.$isResolve()
        } else {
          this.$isResolve()
        }
      }
      // #endif
      // #ifndef MP-WEIXIN
      let res = await getSignIn2()
      if (res.code === 200) {
        res.data ? uni.setStorageSync('token', res.data.accessToken) : ''
        console.log('登录成功')
        this.$isResolve()
      }
      // #endif
    },
    // 业务员登录
    async salespersonLogin () {
      uni.removeStorageSync('token')
      let r = await salesmanGetSignIn2()
      if (r.code === 200) {
        r.data ? uni.setStorageSync('token', res.data.accessToken) : ''
        console.log('业务员登录成功')
        this.$isResolve()
      }
    },
    hasToken () {
      return uni.getStorageSync('token') ? true : false
    },
    verifyTokenStatus () {
      let c = this.hasToken()
      if (!c) return false
      let a = JSON.parse(jwtDecode(uni.getStorageSync('token')).userContext).purchaserApplyStatus
      let b = uni.getStorageSync('token') ? true : false
      // false 已通过不显示 true 未通过显示资质
      return a === 'PASS' || a === 'REVIEW' || !b ? false : true
    }
  }
}
</script>

<style lang="scss">
* {
  margin: 0;
  padding: 0;
}

ul,
li {
  list-style: none;
}

page {
  background-color: #f9f9f9;
}

button {
  overflow: hidden;
}

/*每个页面公共css */

//售罄
.sellOut_container-img {
  // position: relative;

  ::before {
    content: "售罄";
    position: absolute;
    left: 50%;
    top: 50%;
    font-size: 15px;
    white-space: nowrap;
    color: rgba(255, 255, 255, 0.4);
    transform: translate(-50%, -50%);
    z-index: 2;
  }

  ::after {
    content: "";
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    background-color: rgba(0, 0, 0, 0.2);
    width: 96rpx;
    height: 96rpx;
    z-index: 1;
  }
}

.sellOut_container-img1 {
  // position: relative;

  ::before {
    content: "售罄";
    position: absolute;
    left: 50%;
    top: 50%;
    font-size: 20px;
    white-space: nowrap;
    color: rgba(255, 255, 255, 0.4);
    transform: translate(-50%, -50%);
    z-index: 2;
  }

  ::after {
    content: "";
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);
    border-radius: 50%;
    background-color: rgba(0, 0, 0, 0.2);
    width: 156rpx;
    height: 156rpx;
    z-index: 1;
  }
}

//图片公告
.inform_announcement {
  position: absolute;
  top: 0;
  left: 0;
  z-index: 3;
}

.singleRowEllipsis {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.priceRow {
  text {
    font-size: 20rpx;
  }
}

.voucher {
  display: inline-block;
  width: 30rpx;
  height: 26rpx;
  line-height: 26rpx;
  font-size: 18rpx;
  font-weight: 400;
  color: #ec1818;
  border: 1px solid #ec1818;
  border-radius: 6rpx;
  text-align: center;
}

.seckillType {
  display: inline-block;
  height: 28rpx;
  line-height: 26rpx;
  font-size: 18rpx;
  color: #fff;
  background: linear-gradient(-51deg, #ec1818, #fc7727);
  border-radius: 50rpx 50rpx 50rpx 0rpx;
  text-align: center;
  padding: 2rpx 8rpx 0;
  margin-right: 7rpx;
}

/* #ifdef MP-TOUTIAO */
checkbox {
  transform: scale(0.6);
  align-items: initial;
}

/* #endif */

checkbox .wx-checkbox-input {
  border-radius: 50% !important;
  transform: scale(0.6);
}

checkbox .wx-checkbox-input.wx-checkbox-input-checked {
  color: #fff;
  background: #2871fc;
}

.ephedrine {
  display: inline-block;
  // width: 48rpx;
  // height: 28rpx;
  // line-height: 26px;
  padding: 4rpx 6rpx;
  text-align: center;
  font-size: 16rpx;
  white-space: nowrap;
  border-radius: 6rpx;
  color: #ef231b !important;
  background: #ffe0df;
  margin-right: 13.89rpx;
}

::-webkit-scrollbar {
  width: 0;
  height: 0;
  color: transparent;
}

::-webkit-scrollbar {
  display: none;
}

::-webkit-scrollbar-track-piece {
  background-color: transparent;
}

/* #ifndef MP */
//选择框样式调整
::v-deep uni-checkbox .uni-checkbox-input {
  transform: scale(0.7);
  border-radius: 50%;
}

::v-deep uni-checkbox .uni-checkbox-input.uni-checkbox-input-checked {
  background-color: #2871fc !important;
  color: #fff !important;
}

::v-deep uni-checkbox:not([disabled]) .uni-checkbox-input:hover {
  border-color: #d1d1d1;
}

/* #endif */
</style>
